//
//  MenuViewController.m
//  ProjectA
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/6.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "MenuViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.titleString;
    self.materialArray = [NSMutableArray array];
    self.materialDic = [NSMutableDictionary dictionary];
    
    NSString *string = [self.urlString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSString *NetString = [NSString stringWithFormat:@"http://68.168.105.137/cookbook/master/master.php?email=&appname=com.riatech.cookbook&country=CN&devid=20e94d365e82aa2f&simcountry=cn&version=7.9.2&versioncode=40&lang=zh-CN&inputlang=zh-CN&lurl=%@", string];
    NSLog(@"//////%@", NetString);
    
     [AFNDownLoader getDataDownLoad:NetString block:^(id myData) {
         
         NSMutableDictionary *BigDic = myData;
         NSLog(@"54321%@", BigDic);
         NSMutableArray *array = [BigDic objectForKey:@"list"];
         
         for (NSMutableDictionary *dic in array) {
             
             classifyModel *classify = [[classifyModel alloc] init];
             [classify setValuesForKeysWithDictionary:dic];
             [self.materialArray addObject:classify];
             
         }
         
         [self.HUD removeFromSuperview];
       
         [self.collectionView reloadData];
         
     }];
    
    UICollectionViewFlowLayout *viewLayout = [[UICollectionViewFlowLayout alloc] init];
    
    if (self.view.frame.size.width == 320) {
        NSLog(@"start");
        // 设置item的大小
        viewLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2.3, self.view.frame.size.width / 2.3);
        // 设置滑动方向
        viewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        // 设置边距
        viewLayout.sectionInset = UIEdgeInsetsMake(14, 14, 14, 14);
        // 设置距离底部大小
        viewLayout.footerReferenceSize = CGSizeMake(50, 50);
        
    }
    
    
    if (self.view.frame.size.width == 375) {
        NSLog(@"start1");
        
        // 设置item的大小
        viewLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2.25, self.view.frame.size.width / 2.25);
        // 设置滑动方向
        viewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        // 设置边距
        viewLayout.sectionInset = UIEdgeInsetsMake(14, 14, 14, 14);
        // 设置距离底部大小
        viewLayout.footerReferenceSize = CGSizeMake(50, 50);
    }if (self.view.frame.size.width == 414) {
        viewLayout.itemSize = CGSizeMake(self.view.frame.size.width / 2.25, self.view.frame.size.width / 2.25);
        // 设置滑动方向
        viewLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        // 设置边距
        viewLayout.sectionInset = UIEdgeInsetsMake(14, 14, 14, 14);
        // 设置距离底部大小
        viewLayout.footerReferenceSize = CGSizeMake(50, 50);
    }
    
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds]collectionViewLayout:viewLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.HUD.labelText = @"加载中";
    [self.view addSubview:self.HUD];
    
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
        [self showtime];
    } completionBlock:^{
        
        
    }];
    
    // 注册cell
    [self.collectionView registerClass:[MenuCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    
    // Do any additional setup after loading the view.
}



-(void)showtime
{
    sleep(10);
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.materialArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath] ;
    classifyModel *classify = [self.materialArray objectAtIndex:indexPath.row];
    NSString *urlstr = [classify.img stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlstr];
    
    [cell.cellImage  sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeholder_Phone@2x.png"]];
    
    cell.cellLabel.text = classify.name;
    
    return cell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    if ([wifi currentReachabilityStatus] == NotReachable || [conn currentReachabilityStatus] == NotReachable) {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"网络不佳, 请稍后重试" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alterView show];
    }

    DetailViewController *detailVC = [[DetailViewController alloc] init];
    classifyModel *classify = [self.materialArray objectAtIndex:indexPath.row];
    detailVC.Labelstr1 = classify.name;
    detailVC.LabelStr2 = classify.img;
    detailVC.LabelStr3 = classify.url;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
